%%%-------------------------------------------------------------------
%%% File    : hiberl_drv_mysql.erl
%%% Author  : alfonso <alfonso@alfonso-laptop>
%%% Description : 
%%%
%%% Created :  9 Oct 2009 by alfonso <alfonso@alfonso-laptop>
%%%-------------------------------------------------------------------
-module(hiberl_drv_mysql).

-export([pk/3, fk/3]).

pk(Odbc, Db, Table) ->
    SQL = [ 
	" SELECT "
	"   CAST(k.column_name AS CHAR)"
	" FROM  "
	"   information_schema.table_constraints t JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) "
	" WHERE "
	"   t.constraint_type='PRIMARY KEY' "
	"   AND t.table_schema='",Db,"'  AND t.table_name='",Table,"'; "],
    {selected,_,PKs} = odbc:sql_query(Odbc, SQL),
    [ PK || {PK} <- PKs].
    
fk(Odbc, Db, Table)->
    SQL = [ 
	" SELECT "
        "   CAST(k.column_name AS CHAR), CAST(k.REFERENCED_TABLE_NAME AS CHAR), CAST(k.REFERENCED_COLUMN_NAME AS CHAR)"
	" FROM  "
	"   information_schema.table_constraints t JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) "
	" WHERE "
	"   t.constraint_type='FOREIGN KEY' "
	"   AND t.table_schema='",Db,"'  AND t.table_name='",Table,"'; "],
    {selected,_,FK} = odbc:sql_query(Odbc, SQL),
    FK.

